#define _CRT_SECURE_NO_WARNINGS 1
#include"head.h"

void CountSort(int* a, int n)
{
	int min = a[0], max = a[0];
	for (int i = 0; i < n; i++)
	{
		if (min > a[i])
			min = a[i];
		if (max < a[i])
			max = a[i];
	}

	int num = max - min + 1;
	int* count = (int*)calloc(num, sizeof(int));

	for (int i = 0; i < num; i++)
	{
		count[a[i] - min]++;
	}

	int j = 0;
	for (int i = 0; i < num; i++)
	{
		while (count[i]--)
		{
			a[j++] = i + min;
		}
	}
	free(count);
}
